<template>
  <div class="">
    <el-card v-if="contentView">
      <el-row>
        <el-col :span="12">
          <Searchmodule :placeholder="placeholder" @seacherKey="seacher"></Searchmodule>
        </el-col>
        <el-col :span="12" style="text-align: right;">
          <div>
            <el-button   type="primary" icon="el-icon-plus" @click="operationView(0)">添加</el-button>
          </div>
        </el-col>
      </el-row>
      <!--表格-->
      <el-table  :row-style="{height: '10px'}" :cell-style="{padding:'0px'}" :data="tableData" border v-loading="loading">
      <!--  <el-table-column type="index" label="序号" v-if="false" align="center" width="50">
        </el-table-column> -->
        <el-table-column prop="repositoryName" label="仓库名称" align="center" >
        </el-table-column>
        <el-table-column prop="repositoryAddress" label="仓库地址" align="center">
        </el-table-column>
        <el-table-column prop="manageUserName" label="仓库管理员" align="center" >
        </el-table-column>
        <el-table-column prop="creatorName" label="创建人" align="center" >
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" >
        </el-table-column>
		<el-table-column  label="操作" align="center" >
			<template  slot-scope="scope">
				<div  class="optBtn-box" >
					<el-button type="text" icon="el-icon-edit" @click="operationView(1, scope.row)">编辑</el-button>
					<el-button type="text" :loading="scope.row.loading" icon="el-icon-delete" @click="operationView(2, scope.row)">删除</el-button>
				</div>
			</template>
		</el-table-column>
      </el-table>
      <el-pagination style="text-align: center;" :disabled="loading" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pageNo" :page-sizes="[25,50]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
    <submitForm v-else :subData="subData" @closeView="closeView" @refresh="refreshTable"></submitForm>
  </div>
</template>

<script>
import { pageRepository, deleteRepository } from '@/api/essentialInformation.js'
import submitForm from './warehouseDetails/addWarehouse.vue'
import { mapState } from "vuex";
let $this = null
export default {
  name: 'purchase',
  components: {
    submitForm
  },
   computed: {
    ...mapState(["authList"]),
  },
  data () {
    return {
      contentView: true,
      placeholder: '仓库名称',
      loading: false,
      queryInfo: {
        pageNo: 1,
        pageSize: 25,
        repositoryName: ''
      },
      tableData: [],
      total: 0,
	  subData: {
		  btnId: 0,
		  id: ''
	  }
    }
  },
  created () {
    $this = this
    $this.getPageRepository()
  },
  methods: {
    authFilter (code) {
      let flag = false
      if(this.authList.length) {
        this.authList.forEach(item => {
          if(item.resourceCode == code) {
            flag = true
          }
        })
      }
      return flag
    },
    getPageRepository () {
      $this.loading = true
      pageRepository($this.queryInfo).then(res => {
        $this.loading = false
        $this.tableData = res.data.data.records
        $this.total = res.data.data.total
      }).catch(err => {
        $this.loading = false
        return $this.$message.error(err.message)
      })
    },
    handleCurrentChange (val) {
      $this.queryInfo.pageNo = val
      $this.getPageRepository()
    },
    handleSizeChange (size) {
      $this.queryInfo.pageSize = size
      $this.getPageRepository()
    },
    seacher (key) {
      $this.queryInfo.repositoryName = key
      $this.getPageRepository()
    },
    operationView (btnId, row) {
      if (btnId === 0) {
        $this.subData.btnId = btnId
        $this.contentView = false
      } else if (btnId === 1) {
        $this.subData.btnId = btnId
        $this.subData.id = row.id
        $this.contentView = false
      } else if (btnId === 2) {
        $this.$set(row, 'loading', true)
        $this.deleteRepository(row)
      } else {
        return $this.$message.error('操作错误')
      }
    },
    // 删除
    deleteRepository (row) {
      deleteRepository(row.id).then(res => {
        $this.$set(row, 'loading', false)
        $this.getPageRepository()
        return $this.$message.success('删除成功')
      }).catch(err => {
        $this.$set(row, 'loading', false)
        return $this.$message.error('操作错误')
      })
    },
    // 返回table
    closeView () {
      $this.contentView = true
    },
    refreshTable () {
      $this.getPageRepository()
      $this.contentView = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
